package com.antoco.operation.bigscreen.service.autogeneration.impl;

import com.antoco.operation.bigscreen.repository.SuiziSensorNumRepository;
import com.antoco.operation.bigscreen.model.entity.SuiziSensorNum;
import com.antoco.operation.bigscreen.model.entity.TjIns;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SuiziSensorNumService;
import com.antoco.operation.bigscreen.utils.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class SuiziSensorNumServiceImpl extends AbstractModelServiceImpl implements SuiziSensorNumService {

    @Autowired
    private SuiziSensorNumRepository repository;

    @Override
    public SuiziSensorNum save(SuiziSensorNum entity) {
        TjIns tjIns = BeanUtils.saveTjIns(SuiziSensorNum.class);
        entity.setTjInsId(tjIns.getId());
        return repository.save(entity);
    }

    @Override
    public List<SuiziSensorNum> save(List<SuiziSensorNum> entities) {
        return (List<SuiziSensorNum>) repository.saveAll(entities);
    }

    @Override
    public void deleteById(String id) {
        repository.deleteById(id);
    }

    @Override
    public Optional<SuiziSensorNum> findById(String id) {
        return repository.findById(id);
    }

    @Override
    public List<SuiziSensorNum> findAll() {
        return (List<SuiziSensorNum>) repository.findAll();
    }

    @Override
    public Page<SuiziSensorNum> findAll(Pageable pageable) {
        Page<SuiziSensorNum> entityPage = repository.findAll(pageable);
        List<SuiziSensorNum> entities = entityPage.getContent();
        return new PageImpl<>(entities, pageable, entityPage.getTotalElements());
    }

    @Override
    public SuiziSensorNum update(SuiziSensorNum entity, String id) {
        Optional<SuiziSensorNum> optional = findById(id);
        if (optional.isPresent()) {
            return save(entity);
        }
        return null;
    }
}